package com.szxy.service.impl;

import com.szxy.mapper.*;
import com.szxy.pojo.*;
import com.szxy.service.*;
import com.szxy.utils.GoodsGrid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther:zwer
 * @Date:2019/9/2 10:51
 * @Description:com.szxy.service.impl
 * @Version:1.0
 **/
@Service
public class ConcreteGoodsServiceImpl implements GoodsService,
        GoodsNoticeService, GoodsImageService,
        GoodsCommentsService, GoodsCatelogService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsNoticeMapper noticeMapper;
    @Autowired
    private GoodsImageMapper imageMapper;
    @Autowired
    private GoodsCommentsMapper commentsMapper;
    @Autowired
    private GoodsCatelogMapper catelogMapper;

    @Override
    public List<Catelog> findAllCatelog() {
        return this.catelogMapper.selAllCatelogmMapper();
    }

    @Override
    public Catelog findCatelogByIdService(Integer id) {
        return this.catelogMapper.selCatelogByIdMapper(id);
    }

    @Override
    public List<Comments> findCommentsByGoodsIdService(Integer goodsId) {
        return this.commentsMapper.selCommentsByGoodsId(goodsId);
    }

    @Override
    public List<Comments> findCommentsByUserIdService(Integer userId) {
        return this.commentsMapper.selCommentsByUserId(userId);
    }

    @Override
    public boolean addCommentsService(Comments comment) {
        return this.commentsMapper.insertCommentsMapper(comment)>0?true:false;
    }

    @Override
    public Image findGoodsImageByGoodIdService(Integer goodId) {
        return this.imageMapper.selGoodsImageByGoodId(goodId);
    }

    @Override
    public void addGoodsImageService(Image image) {
        this.imageMapper.insertImageMapper(image);
    }

    @Override
    public List<Notice> findAllNotice() {
        return this.noticeMapper.selAllNoticeMapper();
    }

    @Override
    public void addNotice(Notice notice) {
        this.noticeMapper.addNoticeMapper(notice);
    }

    @Override
    public NoticeGrid findNoticeByPaginationService(Integer pageNum, Integer pageSize) {
        NoticeGrid noticeGrid = new NoticeGrid();
        Integer startPos = (pageNum-1)*pageSize;
        Integer endPos =  pageSize;
        List<Notice> noticeList = this.noticeMapper.selNoticeByPagination(startPos,endPos);
        noticeGrid.setRows(noticeList);
        noticeGrid.setCurrent(pageNum);
        noticeGrid.setRowCount(pageSize);
        noticeGrid.setTotal(this.noticeMapper.selAllNoticeCount());
        return noticeGrid;
    }

    @Override
    public List<Goods> findByCatelogGoodsIdService(Integer cateid, Integer limit) {
        return this.goodsMapper.selByCatelogGoodsId(cateid,limit);
    }

    @Override
    public Goods findByGoodIdService(Integer id) {
        return this.goodsMapper.selByGoodId(id);
    }

    @Override
    public void addGoodsService(Goods goods) {
        this.goodsMapper.insertGoodsMapper(goods);
    }

    @Override
    public List<Goods> searchGoodsService(String str) {
        return this.goodsMapper.findGoodsByNameMapper(str);
    }

    @Override
    public List<Goods> findUserPublishedAllGoodsService(Integer userId) {
        return this.goodsMapper.selUserPublishedAllGoodsMapper(userId);
    }

    @Override
    public void updateGoodsService(Goods goods) {
        this.goodsMapper.updateGoodsMapper(goods);
    }

    @Override
    public void deleteGoodsService(Integer goodId) {
        this.goodsMapper.delGoodsMapper(goodId);
    }

    @Override
    public GoodsGrid findGoodsByPagination(Integer page, Integer pageSize) {
        GoodsGrid goodsGrid = new GoodsGrid();
        goodsGrid.setCurrent(page);
        goodsGrid.setRowCount(pageSize);
        Integer start = (page-1)*pageSize;
        Integer end = pageSize;
        List<Goods> goodsList = this.goodsMapper.selGoodsByPaginationMapper(start,end);
        goodsGrid.setRows(goodsList);
        int total = this.goodsMapper.selGoodsAllCountMapper();
        goodsGrid.setTotal(total);
        return goodsGrid;
    }
}
